
#include <stdio.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h> 

int listenfd1;
volatile int read_flag = 0;
static char buf[256] = { 0 };
void do_sigio(int sig)
{
    struct sockaddr_in cli_addr;
    int clifd, clilen;
    read_flag = 1;
    //clifd = accept(listenfd1, (struct sockaddr *) &cli_addr, &clilen);
    memset(buf, 0, sizeof(buf));
    recvfrom(listenfd1, buf, sizeof(buf), 0, (struct sockaddr *)&cli_addr, &clilen);
    printf("Listenfd1 Message %s \n", buf);
    perror("recvfrom");
    sendto(listenfd1, "Reply", sizeof("Reply"),0, (struct sockaddr *)&cli_addr, sizeof(cli_addr));
    perror("sendto");
    printf("sigio end\n");
    read_flag = 0;
}

 
int main(int argc, char *argv[])
{
    //绑定监听7779端口的fd
    struct sockaddr_in serv_addr;
    listenfd1 = socket(AF_INET, SOCK_DGRAM, 0);
     
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(7779);
    serv_addr.sin_addr.s_addr = INADDR_ANY;
     
    struct sigaction sigio_action;
    memset(&sigio_action, 0, sizeof(sigio_action));
    sigio_action.sa_flags = 0;
    sigio_action.sa_handler = do_sigio;
    sigaction(SIGIO, &sigio_action, NULL);
#if 1
    fcntl(listenfd1, F_SETOWN, getpid());
    int flags;
    flags = fcntl(listenfd1, F_GETFL, 0);
    flags |= O_ASYNC ;//| O_NONBLOCK;
    fcntl(listenfd1, F_SETFL, flags);
#endif
 
    bind(listenfd1, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
 
    while(1)
    {
        sleep(2);
    }
    close(listenfd1);
 
    return 0;
 
}
